package pers.vic.practice.module.index.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.util.CookieUtil;
import pers.vic.boot.util.JsonUtil;
import pers.vic.practice.base.constant.BaseConstant;
import pers.vic.practice.base.enums.UserType;
import pers.vic.practice.base.model.Manager;
import pers.vic.practice.base.model.Principal;
import pers.vic.practice.module.student.model.SStudent;
import pers.vic.practice.module.student.service.SStudentService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
import java.math.BigInteger;

/**
 * 描述:
 * @author Vic.xu
 * @date 2022-04-12 10:15
 */
@Service
public class IndexService {

    @Resource
    private SStudentService studentService;


    /**
     * 登录校验 并存入到Cookie
     *
     * @param username 管理员账号 或学号
     * @param password
     * @return
     */
    public BaseResponse<Principal> login(UserType userType, String username, String password) {
       switch (userType){
           case manager:
               return managerLogin(username, password);
           case student:
           default:
               return studentLogin(username, password);
       }
    }

    private BaseResponse<Principal> studentLogin(String username, String password) {
        //判断是否是学生
        SStudent student = studentService.findByNumber(username);
        if (student == null || !StringUtils.equals(username, password)) {
            return BaseResponse.error("账号或密码错误");
        }
        //构建学生登录信息
        Principal principal = Principal.buildStudent(student);
        return BaseResponse.success(principal);
    }


    private BaseResponse<Principal> managerLogin(String username, String password){
        if (BaseConstant.MANAGER_USER_NAME.equals(username)) {
            Manager manager = Manager.virtualManager();
            if (!manager.getPassword().equals(password)) {
                return BaseResponse.error("账号或密码错误");
            }
            //构建管理员登录信息
            Principal principal = Principal.buildManager(manager);
            return BaseResponse.success(principal);
        }
        return BaseResponse.error("账号或密码错误");
    }



    /**
     * 根据cookie判断当前是否已经登录
     *
     * @param request
     * @return
     */
    public Principal ifLogin(HttpServletRequest request) {
        String cookieToken = CookieUtil.getValue(request, BaseConstant.USER_SESSION_KEY);
        if (cookieToken != null) {
            Principal cookieUser = parseToken(cookieToken);
            return  cookieUser;
        }
        return null;
    }

    /**
     * 把user转为token
     *
     * @param user
     * @return
     */
    public String makeToken(Principal user) {
        String tokenJson = JsonUtil.toJson(user);
        String tokenHex = new BigInteger(tokenJson.getBytes()).toString(16);
        return tokenHex;
    }

    /**
     * 把token转为user
     *
     * @param tokenHex
     * @return
     */
    public Principal parseToken(String tokenHex) {
        Principal user = null;
        if (tokenHex != null) {
            String tokenJson = new String(new BigInteger(tokenHex, 16).toByteArray()); // username_password(md5)
            user = JsonUtil.jsonToObject(tokenJson, Principal.class);
        }
        return user;
    }

}
